export default {
	data() {
		return {
			indexSocketTask: '',
			//登录状态
			tin: '',
			//群聊消息提示
			tip: '',
			keyword: '',
			userId: 0,
			iconStyle: {
				fontSize: '32rpx',
				color: '#2979ff'
			},
			text: '',
			value: 1,

			//聊天列表
			indexList: [],
			//聊天列表加载动画
			isIndexList: true,
			//搜索出的聊天列表
			searchList: '',

			downList: [{
					iconPath: "home",
					selectedIconPath: "home-fill",
					text: '消息',
					customIcon: false,
					pagePath: '/pages/index/index'
				},
				{
					iconPath: "man-add",
					selectedIconPath: "man-add-fill",
					text: '添加',
					customIcon: false,
					pagePath: '/pages/add/add'
				},
				{
					iconPath: "account",
					selectedIconPath: "account-fill",
					text: '我的',
					customIcon: false,
					pagePath: '/pages/user/user'
				}
			],
			current: 0
		}
	},
	onLoad() {
		this.getGroup();
	},
	onShow() {
		uni.closeSocket();
		this.indexLink();
	},
	methods: {
		async beforeSwitch(index) {
			console.log(index)
			switch (index) {
				case 0:
					//this.indexLink();
					break;
				case 1:
					uni.closeSocket()
					break;
				case 2:
					uni.closeSocket()
					break;
			}
			return true;
		},
		//首页群聊消息监听
		indexLink() {
			var that = this;
			that.indexSocketTask = uni.connectSocket({
				url: this.$ws + '/IndexLink/' + this.userId,
				success: () => {
					// 返回实例
					return that.indexSocketTask;
				},
			});
			that.indexSocketTask.onOpen(function(res) {
				console.log("index_link_success")
				that.tin = '在线';
			});
			that.indexSocketTask.onError(function(res) {
				that.tin = '离线';
			});
			that.indexSocketTask.onMessage(function(res) {
				if (res.data == "200") {
					console.log("200")
				} else {
					var date = new Date();
					var data = JSON.parse(res.data);
					that.indexList = data;

					//未读消息提醒
					for (let i in res.data) {
						if (data[i]) {
							var num = data[i].chatNum - data[i].lookNum;
							if (num >= 99) {
								that.indexList[i].tip = 99
							}
							if (num > 0 && num < 99) {
								that.indexList[i].tip = num;
							}
							if (num <= 0) {
								that.indexList[i].tip = ''
							}
						}
						else {
							continue;
						}
					}
				}
			});
		},
		//进入群聊
		gotoChat(e) {
			//清空群聊未读信息数
			uni.navigateTo({
				url: '/pages/chat/chat?groupId=' + e.id + '&groupName=' + e.groupName + '&groupImg=' + e
					.groupImg,
			});
		},
		//发送消息
		sendMsg() {
			var msg = "400";
			try {
				if (this.indexSocketTask) {
					this.indexSocketTask.send({
						data: msg
					});
				}
			} catch (e) {
				this.indexSocketTask.close({
					success() {
						uni.showToast({
							title: '连接已断开',
							icon: "none"
						});
					}
				});
			}
		},
		//搜索群组
		search(e) {
			if (e == '') {
				this.searchList = [];
				return;
			}
			this.$request('/Group/queryUserGroupByKeyword', {
				// 传参参数名：参数值,如果没有，就不需要传
				"keyword": e,
				"userId": this.userId
			}).then(res => {
				if (res.code == 200) {
					this.searchList = res.data;
				}
			})
		},
		//获取群组列表
		getGroup() {
			this.userId = uni.getStorageSync('userId');
			this.$request('/Group/queryGroupByUserId', {
				// 传参参数名：参数值,如果没有，就不需要传
				"userId": this.userId
			}).then(res => {
				if (res.code == 200) {
					this.indexList = res.data;
					this.isIndexList = false;
				}
			})
		}
	}
}
